﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

public partial class Admin_Components_Inventory_Warehouse : System.Web.UI.UserControl
{
    DBDataContext db;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] != null)
        {
            if (!IsPostBack)
                ResetForm();
        }
        else
        {
            Response.Redirect("~/Default.aspx", false);
        }
    }
   
    private void ResetForm()
    {
        try
        {
            //Cửa hàng chuyển
            string strShopID = string.Empty;
            //if (Session["RoleID"] == null || Session["RoleID"].ToString() == "1")
            //    strShopID = "0";
            //else strShopID = Session["ShopID"].ToString();
            strShopID = Session["ShopID"].ToString();
            if (strShopID == "0")// admin
            {
                Utils.loadShopToComboSearch(cbShop, 1, Convert.ToInt32(strShopID));//Có tất cả
            }
            else//Khong phai admin
            {
                Utils.loadShopToComboSearch(cbShop, 0, Convert.ToInt32(strShopID));// Không có tất cả
            }
            Utils.loadProductToComboSearch(cbProduct, 1, "");
            gvData.DataSource = new DataTable();
            gvData.DataBind();
        }
        catch (Exception ex)
        {
            ControlUtil.LogError("Warehouse Page-loadForm:" + ex.Message);
        }
    }
    private void getDataSearch()
    {
        db = new DBDataContext();
        string strShopID = cbShop.SelectedValue.ToString();
        string strProductID = cbProduct.SelectedValue.ToString();
        string strType = cbType.SelectedValue.ToString();
        strShopID = strShopID == "" ? "-1" : strShopID;
        strProductID = strProductID == "" ? "-1" : strProductID;

        var listProduct = (from a in db.Warehouses
                           join b in db.Products on a.ProductID equals b.ID
                           join c in db.Shops on a.ShopID equals c.ID
                           where a.Status == 'A'
                                && (a.ShopID == Convert.ToInt32(strShopID) || Convert.ToInt32(strShopID) == -1)
                                && (a.ProductID == Convert.ToInt32(strProductID) || Convert.ToInt32(strProductID) == -1)
                                && (b.ProductType == strType || strType == "")
                           //&& a.Quantity > 0
                           orderby b.ProductName
                           select new
                           {
                               ProductName = b.ProductCode + "-" + b.ProductName + (a.PackType == 0 ? "" : "(" + a.PackType.ToString() + " kg)"),
                               ProductID = b.ID,
                               ProductCode = b.ProductCode,
                               ShopName = c.ShopCode + "-" + c.ShopName,
                               PurchasePrice = a.PurchasePrice,
                               TotalAmount = a.PurchasePrice * a.Quantity,
                               Weight = a.PackType == 0 ? "" : (a.Quantity * a.PackType).ToString(),
                               Quantity = a.Quantity.ToString()
                           }).Distinct().ToList();

        DataTable dt = listProduct.ToDataTable();
        if (dt.Rows.Count > 0)
        {
            gvData.Height = 250;
            gvData.ClientSettings.Scrolling.AllowScroll = true;
            gvData.ClientSettings.Scrolling.UseStaticHeaders = true;
            gvData.VirtualItemCount = Convert.ToInt32(dt.Rows.Count);
            div_TotalRows.InnerText = "Kết quả tìm kiếm: " + String.Format("{0:N0}",dt.Rows.Count);
            gvData.DataSource = dt;

        }
        else
        {
            gvData.DataSource = new DataTable();
            div_TotalRows.InnerText = "Kết quả tìm kiếm: 0";
        }
        //gvData.DataBind();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        try
        {
            getDataSearch();
            gvData.DataBind();
        }
        catch (Exception ex)
        {
            ControlUtil.LogError("Warehouse Page-btnSearch_Click:" + ex.Message);
            ResetForm();
        }
    }
    protected void cbType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        if (cbType.SelectedValue == "R")
        {
            Utils.loadProductToComboSearch(cbProduct, 1, "R");
        }
        if (cbType.SelectedValue == "P")
        {
            Utils.loadProductToComboSearch(cbProduct, 1, "P");
        }
    }
    protected void gvData_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        try
        {
            //Ham phan trang: Paging
            if (RadPageView3.Selected)
            {
                TabStrip1.SelectedIndex = 3;
                gvData.Height = 250;
                gvData.Visible = true;
                getDataSearch();
            }
            else
            {
                gvData.DataSource = new DataTable();
            }
        }
        catch (Exception ex)
        {
            ControlUtil.LogError("StockTransfer Page-gvData_NeedDataSource:" + ex.Message);
        }
    }
    
}